package com.sxran.im.handler;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sxran.im.bean.ImException;
import com.sxran.im.bean.NotificationBody;
import com.sxran.im.controller.ImController;
import com.sxran.im.dao.*;
import com.sxran.im.info.ImFriend;
import com.sxran.im.info.ImFriendInvite;
import com.sxran.im.info.ImGroupInvite;
import com.sxran.im.info.ImUserGroup;
import com.sxran.im.service.FriendInviteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AddGroupInviteCmdHandler extends AbstractCmdHandler<ImGroupInvite>{
    @Resource
    private ImGroupInviteMapper imGroupInviteMapper;
    @Resource
    private ImUserGroupMapper imUserGroupMapper;


    @Override
    public Object doBeanService(ImGroupInvite bean) {
        String curUserId = ImController.getCurUserId();
        String groupId = bean.getGroupId();


        //
        LambdaQueryWrapper<ImUserGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImUserGroup::getGid,groupId);
        queryWrapper.eq(ImUserGroup::getUid,curUserId);
        if (imUserGroupMapper.exists(queryWrapper)) {
            throw new ImException("已经是成员",10025);
        }
        // TODO 检查是不是黑名单...

        // 不存在就添加，存在就更新
        bean.setUserId(curUserId);

        LambdaQueryWrapper<ImGroupInvite> groupInviteWrapper = new LambdaQueryWrapper<>();
        groupInviteWrapper.eq(ImGroupInvite::getGroupId,groupId);
        groupInviteWrapper.eq(ImGroupInvite::getUserId,curUserId);
        ImGroupInvite imGroupInvite = imGroupInviteMapper.selectOne(groupInviteWrapper);
        if (imGroupInvite == null) {
            imGroupInvite = bean;
            imGroupInvite.setUpdateTime(new Date());
            imGroupInvite.setCreatTime(new Date());
            imGroupInvite.setState(0);
            imGroupInviteMapper.insert(imGroupInvite);
        }else{
            imGroupInvite.setState(0);
            imGroupInvite.setUpdateTime(new Date());
            imGroupInvite.setMessage(bean.getMessage());
            imGroupInviteMapper.update(imGroupInvite,groupInviteWrapper);
        }

        // 获取管理员
        LambdaQueryWrapper<ImUserGroup> notificationWrapper = new LambdaQueryWrapper<>();
        notificationWrapper.eq(ImUserGroup::getGid,groupId);
        notificationWrapper.eq(ImUserGroup::getRole,"admin");
        List<ImUserGroup> imUserGroups = imUserGroupMapper.selectList(notificationWrapper);
        String[] targetList = new String[imUserGroups.size()];
        for (int i = 0; i < targetList.length; i++) {
            targetList[i] = imUserGroups.get(i).getUid();
        }
        // 通知
        NotificationBody notificationBody = new NotificationBody();
        notificationBody.setTargetList(targetList);
        notificationBody.setContent(bean.getMessage());
        // 将群组id放入到扩展字段中。
        JSONObject extras = notificationBody.getExtras();
        extras.put("groupId",groupId);
        return notificationBody;
    }


}
